<template>
  <div class="article-show">
    <!-- 顶部信息栏 -->
    <div class="header-info">
      <div class="post-meta">
        <el-icon><User /></el-icon>
        <span>Posted by {{ articleData.author }}, {{ articleData.publishDate }}</span>
      </div>
    </div>

    <!-- 内容矩阵区域 -->
    <div class="content-matrix">
      <!-- 图片区域 -->
      <div class="image-section">
        <el-image
          :src="articleData.desktopImage"
          class="article-image"
          fit="cover"
          :preview-src-list="[articleData.desktopImage]"
          loading="lazy"
          :alt="articleData.imageAlt"
        />
      </div>

      <!-- 文本内容区域 -->
      <div class="text-section">
        <div class="article-header">
          <div class="article-tags">
            <el-tag 
              v-for="(tag, index) in articleData.tags" 
              :key="index" 
              class="tag-item"
              :type="tagTypes[index % tagTypes.length]"
            >
              {{ tag }}
            </el-tag>
          </div>
          
          <h1 class="article-title">{{ articleData.title }}</h1>
          
          <div class="article-meta">
            <div class="meta-item">
              <el-icon><Clock /></el-icon>
              <span>{{ articleData.readTime }}</span>
            </div>
            <div class="meta-item">
              <el-icon><View /></el-icon>
              <span>{{ articleData.viewCount }} views</span>
            </div>
          </div>
        </div>
        
        <div class="article-content" v-html="articleData.content"></div>
        
        <div class="article-actions">
          <el-button 
            type="primary" 
            :icon="Star" 
            class="action-btn" 
            size="large"
            @click="handleLike"
          >
            <template #default>
              <span class="btn-text">点赞</span>
              <span class="btn-count">{{ articleData.likeCount }}</span>
            </template>
          </el-button>
          <el-button 
            type="success" 
            :icon="Collection" 
            class="action-btn" 
            size="large"
            @click="handleFavorite"
          >
            <template #default>
              <span class="btn-text">收藏</span>
              <span class="btn-count">{{ articleData.favoriteCount }}</span>
            </template>
          </el-button>
          <el-button 
            type="info" 
            :icon="Share" 
            class="action-btn" 
            size="large"
            @click="handleShare"
          >
            <span class="btn-text">分享</span>
          </el-button>
        </div>
        
        <el-divider class="custom-divider" v-if="articleData.enableComments" />
        
        <!-- 评论组件 -->
        <CommentSection 
          :comments="articleData.comments" 
          @add-comment="handleAddComment" 
          v-if="articleData.enableComments" 
        />
      </div>
    </div>
  </div>
  
  <!-- 回到顶部按钮 -->
  <BacktopWithText />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Star, Share, User, Clock, View ,Collection} from '@element-plus/icons-vue'
import CommentSection from '@/components/CommentSection.vue'
import BacktopWithText from '@/components/BacktopWithText.vue'
// 导入api接口
import { getArticle, addComment, likeArticle, favoriteArticle } from '@/utils/api'

// 组件数据
const tagTypes = ref(['primary', 'success', 'warning', 'danger', 'info'])

// 文章数据
const articleData = ref({
  id: '',
  title: '',
  author: '',
  publishDate: '',
  readTime: '',
  viewCount: 0,
  likeCount: 0,
  favoriteCount: 0,
  desktopImage: '',
  imageAlt: '',
  tags: [],
  content: '',
  enableComments: true,
  comments: []
})

// 从API获取文章数据
const fetchArticleData = async () => {
  try {
    const response = await getArticle(1) // 假设文章ID为1
    if (response.data.code === 0) {
      articleData.value = response.data.data
    }
  } catch (error) {
    console.error('获取文章数据失败:', error)
    ElMessage.error('获取文章数据失败')
  }
}

// 点赞处理
const handleLike = async () => {
  try {
    const response = await likeArticle(articleData.value.id)
    if (response.data.code === 0) {
      articleData.value.likeCount = response.data.data.likeCount
      ElMessage.success(response.data.msg)
    }
  } catch (error) {
    console.error('点赞失败:', error)
    ElMessage.error('点赞失败，请重试')
  }
}

// 收藏处理
const handleFavorite = async () => {
  try {
    const response = await favoriteArticle(articleData.value.id)
    if (response.data.code === 0) {
      articleData.value.favoriteCount = response.data.data.favoriteCount
      ElMessage.success(response.data.msg)
    }
  } catch (error) {
    console.error('收藏失败:', error)
    ElMessage.error('收藏失败，请重试')
  }
}

// 处理添加评论
const handleAddComment = async (commentContent) => {
  try {
    const response = await addComment(articleData.value.id, { content: commentContent })
    if (response.data.code === 0) {
      articleData.value.comments.unshift(response.data.data)
      ElMessage.success(response.data.msg)
    }
  } catch (error) {
    console.error('发表评论失败:', error)
    ElMessage.error('发表评论失败，请重试')
  }
}

// 分享处理
const handleShare = () => {
  if (navigator.share) {
    navigator.share({
      title: articleData.value.title,
      text: articleData.value.content.substring(0, 100) + '...',
      url: window.location.href
    }).catch(error => {
      console.log('分享失败:', error)
    })
  } else {
    navigator.clipboard.writeText(window.location.href)
    ElMessage.success('链接已复制到剪贴板')
  }
}



// 组件挂载时获取数据
onMounted(() => {
  fetchArticleData()
})
</script>

<style scoped>
/* 样式保持不变，与之前相同 */
.article-show {
  font-family: 'Inter', 'Helvetica Neue', Arial, sans-serif;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  min-height: 100vh;
}

/* 顶部信息栏样式 */
.header-info {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 1.5rem 0;
  border-bottom: 1px solid #e2e8f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.post-meta {
  max-width: 1060px;
  margin: 0 auto;
  padding: 0 2rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #64748b;
  font-size: 0.9rem;
  font-weight: 500;
}

/* 图片区域样式 */
.image-section {
  background: white;
  padding: 3rem 0;
  display: flex;
  justify-content: center;
}

.article-image {
  width: 100%;
  max-width: 1160px;
  height: 400px;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.article-image:hover {
  transform: scale(1.02);
}

/* 文本内容区域样式 */
.text-section {
  background: white;
  margin: -50px auto 3rem;
  padding: 3rem;
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.08);
  max-width: 1060px;
  position: relative;
  z-index: 2;
}

.article-header {
  margin-bottom: 2.5rem;
}

.article-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.tag-item {
  border-radius: 20px;
  padding: 0.5rem 1rem;
  font-weight: 600;
  font-size: 0.85rem;
  border: none;
}

.article-title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #1e293b;
  line-height: 1.3;
  margin: 0 0 1.5rem 0;
  background: linear-gradient(135deg, #1e293b 0%, #475569 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.article-meta {
  display: flex;
  gap: 1.5rem;
  align-items: center;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #64748b;
  font-size: 0.9rem;
  font-weight: 500;
}

.article-content {
  line-height: 1.8;
  font-size: 1.1rem;
  color: #334155;
  margin: 2.5rem 0;
}

.article-content p {
  margin: 1.5rem 0;
  text-align: justify;
}

.article-content strong {
  color: #1e293b;
  font-weight: 600;
}

.article-content a {
  color: #3b82f6;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.3s ease;
}

.article-content a:hover {
  color: #2563eb;
  text-decoration: underline;
}

/* 操作按钮样式 */
.article-actions {
  display: flex;
  gap: 1rem;
  margin: 2.5rem 0;
  justify-content: center;
}

.action-btn {
  border-radius: 12px;
  padding: 0.75rem 1.5rem;
  font-weight: 600;
  transition: all 0.3s ease;
  border: 2px solid transparent;
  margin-left: 0;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

.btn-text {
  margin: 0 0.5rem;
}

.btn-count {
  background: rgba(255, 255, 255, 0.2);
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  margin-left: 0.5rem;
  font-weight: 600;
}

.custom-divider {
  margin: 3rem 0;
  border-color: #e2e8f0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .post-meta {
    padding: 0 1rem;
    justify-content: center;
  }
  
  .article-image {
    height: 300px;
    border-radius: 12px;
  }
  
  .text-section {
    margin-top:0.1rem;
    padding: 2rem 1.5rem;
    border-radius: 20px;
  }
  
  .article-title {
    font-size: 2rem;
  }
  
  .article-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }
  
  .action-btn {
    width: 100%;
  }
  
  .article-meta {
    flex-direction: column;
    gap: 0.75rem;
    align-items: flex-start;
  }
}
</style>